package com.lijinjiang.request;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @ClassName URLDemo
 * @Description TODO
 * @Author Li
 * @Date 2022/9/24 22:21
 * @ModifyDate 2022/9/24 22:21
 * @Version 1.0
 */
public class URLDemo {
    public static void main(String[] args) throws Exception {
        String username = "张三";
        // 1.URL编码
        String encode = URLEncoder.encode(username, "UTF-8");
        System.out.println(encode);

        // 2.URL解码
        //String decode = URLDecoder.decode(encode, "UTF-8");
        String decode = URLDecoder.decode(encode, "ISO-8859-1");
        System.out.println(decode);

        // 3.转换为字节数组，编码
        byte[] bytes = decode.getBytes(StandardCharsets.ISO_8859_1);
        for (byte b : bytes) {
            System.out.print(b + " ");
        }
        System.out.println();

        // 4.将字节数组转成字符串，解码
        String s = new String(bytes, StandardCharsets.UTF_8);
        System.out.println(s);
    }
}
